#include <iostream>
#include <vector>
#include <string>
using namespace std;
void select_sort(int *arr, int length)
{
    // arr: an array of int vars ,length: length of arr
    for (int i = 0; i < length - 1; ++i)
    {
        int min = i + 1; //min : index of smallest number after i
        for (int j = i; j < length; ++j)
        {
            if (arr[j] < arr[min])
            {
                min = j;
            }
        }
        // cout << "min:"<<min<<endl;
        int temp = arr[i]; // exchange min and i ;
        arr[i] = arr[min];
        arr[min] = temp;
    }
}
vector<string> trans_string_to_vector(string str)
{
    // int index = str.find(' ');
    // string str1=
    vector<string> str_vec;
    while (str.find(' ') != std::string::npos)
    {
        // cout <<str.find(' ')<<endl;
        string str1 = str.substr(0, str.find(' '));
        str = str.substr(str.find(' ') + 1, str.length());
        // cout << str1 << endl;
        str_vec.push_back(str1);
    }
    str_vec.push_back(str);
    return str_vec;
}
int string_comapre(string str1, string str2)
{
    // implement std::string::compare
    int len1 = str1.length();
    int len2 = str2.length();
    for (int i = 0; i < (len1 < len2 ? len1 : len2); ++i)
    {
        // loop with the shorter string
        if (str1[i] < str2[i])
        {
            return 1;
        }
        else if (str1[i] > str2[i])
        {
            return -1;
        }
        else
            continue;
    }
    if (len1 < len2)
    {
        return 1;
    }
    else
    {
        return -1;
    }
}
void sort_str_vec_by_char(vector<string> &str_vec)
{
    for (int i = 0; i < str_vec.size() - 1; ++i)
    {
        // select sort by initial letters
        int min = i + 1;
        for (int j = i; j < str_vec.size(); ++j)
        {
            if (string_comapre(str_vec[j], str_vec[min]) == 1)
            {
                min = j;
            }
        }
        swap(str_vec[min], str_vec[i]);
    }
}
void delete_arr(char* char_arr, int length,int index)
{
    for(int j =index;j<length-1;++j)
    {
        char_arr[j]=char_arr[j+1];
        // cout << char_arr[j];
    }
    // cout <<endl;
    char_arr[length-1]='\0';
}
int fun (char* char_arr,int length)
{
    int to_delete[length];
    int count_delete=0;
    for(int j=0;j<length;++j)
    {
        if(char_arr[j]=='*')
            continue;   //filter * before the first character
        else{ 
            // when hit the first character,mark the index of '*' after that
            for (int i = j+1;i<length;++i)
            {
                if(char_arr[i]=='*')
                    {
                       to_delete[count_delete]=i;
                       count_delete++;
                    //    cout << i<<endl;
                    }
            }
            break;
        }
    }
    //delete * 
    for(int j=0;j<count_delete;++j)
    {
        delete_arr(char_arr,length,to_delete[j]-j);
        length--;
    }
    return length;
}
int main(int, char **)
{
    std::cout << "Assignment1:\n";
    int input[5] = {4, 3, 2, 1, 6};
    std::cout << "before sort :\n";
    for (int i = 0; i < 5; ++i)
        cout << input[i] << endl;
    select_sort(input, 5);
    std::cout << "after sort :\n";
    for (int i = 0; i < 5; ++i)
        cout << input[i] << endl;
    std::cout << "Assignment2:\n";
    // string str="asdas";
    cout << "Input : " << endl;
    string str = "abc dfn ars asdc sadc";
    cout <<str<<endl;
    vector<string> str_vec = trans_string_to_vector(str);
    sort_str_vec_by_char(str_vec);
    cout << "Output : " << endl;
    for (int i = 0; i < str_vec.size(); ++i)
        cout << str_vec[i] << " ";
    cout << endl;
    std::cout << "Assignment3:\n";
    int length=17;
    char A3[]="***123**213*asd**";
        cout<<"before function fun:" <<endl;
    for (int j= 0 ;j<length;++j)
    {
        cout << A3[j];
    }
    cout <<endl;
    int new_lengh= fun(A3,length);
            cout<<"after function fun:" <<endl;
    for (int j= 0 ;j<new_lengh;++j)
    {
        cout << A3[j];
    }
    cout <<endl;
}
